package org.starsource.forum.client.domain;


import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.starsource.forum.client.util.Assertion;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.IsSerializable;

public class SessionObject implements IsSerializable {

	private int id;
	
	private UserObject owner;
	
	private String title;

	private Timestamp createTimestamp;
	
	private Timestamp updateTimestamp;
	
	private CategoryObject category;
	
	private List<MessageObject> messages = new ArrayList<MessageObject>();
	
	public SessionObject() {
		
	}
	
	public SessionObject(UserObject owner, String title, CategoryObject category, Timestamp createTimestamp, Timestamp updateTimestamp) {
		this.owner = owner;
		this.title = title;
		this.category = category;
		this.createTimestamp = createTimestamp;
		this.updateTimestamp = updateTimestamp;
	}

	public SessionObject(int id, UserObject owner, String title, CategoryObject category, Timestamp createTimestamp, Timestamp updateTimestamp) {
		this.id = id;
		this.owner = owner;
		this.title = title;
		this.category = category;
		this.createTimestamp = createTimestamp;
		this.updateTimestamp = updateTimestamp;
	}

	@Override
	public boolean equals(Object obj) {
		try {
			SessionObject object = (SessionObject)obj;
			
			if (this.id != object.id) return false;
			
			if (!Assertion.isEqual(this.owner, object.owner)) return false;

			if (!Assertion.isEqual(this.title, object.title)) return false;
			
			if (!Assertion.isEqual(this.category, object.category)) return false;
			
			if (!Assertion.isEqual(this.createTimestamp, object.createTimestamp)) return false;
			
			if (!Assertion.isEqual(this.updateTimestamp, object.updateTimestamp)) return false;
			
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public int getId() {
		return this.id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public UserObject getOwner() {
		return this.owner;
	}
	
	public void setOwner(UserObject owner) {
		this.owner = owner;
	}
	
	public Timestamp getCreateTimestamp() {
		return this.createTimestamp;
	}
	
	public void setCreateTimestamp(Timestamp createTimestamp) {
		this.createTimestamp = createTimestamp;
	}
	
	public Timestamp getUpdateTimestamp() {
		return this.updateTimestamp;
	}
	
	public void setUpdateTimestamp(Timestamp updateTimestamp) {
		this.updateTimestamp = updateTimestamp;
	}
	
	public String getTitle() {
		return this.title;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public CategoryObject getCategory() {
		return this.category;
	}
	
	public void setCategory(CategoryObject category) {
		this.category = category;
	}
	
	public List<MessageObject> getMessages() {
		return this.messages;
	}
	
	public void setMessages(List<MessageObject> messages) {
		this.messages = messages;
	}

	public Date getCreateDate() {
		try {
			return new Date(this.createTimestamp.getTime());
		} catch (Exception e) {
			return null;
		}
	}
	
	public Time getCreateTime() {
		try {
			return new Time(this.createTimestamp.getTime());
		} catch (Exception e) {
			return null;
		}
	}
	
	public String getUpdateDateString() {
		if (this.updateTimestamp == null) return null;
		
		DateTimeFormat f = DateTimeFormat.getFormat("yyyy-MM-dd");  
		return f.format(this.updateTimestamp);
	}

	public String getUpdateTimeString() {
		if (this.updateTimestamp == null) return null;
		
		DateTimeFormat f = DateTimeFormat.getFormat("yyyy-MM-dd hh:mm");  
		return f.format(this.updateTimestamp);
	}
	
}
